package cn.jimmie.learn.pbc.util;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.math.BigInteger;
import java.security.MessageDigest;

import cn.jimmie.learn.pbc.base.BaseFragment;

public class Utils {

    /**
     * 把Assets里面得文件复制到 /data/data/files 目录下
     */
    public static void extractAssets(Context context, String pluginName) {
        AssetManager am = context.getAssets();
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            is = am.open(pluginName);
            File extractFile = context.getFileStreamPath(pluginName);
            bos = new BufferedOutputStream(
                    new FileOutputStream(extractFile)
            );
            byte[] buffer = new byte[1024];
            int count;
            while ((count = is.read(buffer)) > 0) {
                bos.write(buffer, 0, count);
            }
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeSilently(is);
            closeSilently(bos);
        }
    }

    private static void closeSilently(Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (Throwable ignored) {
        }
    }

    public static BaseFragment getFragment(String name, ClassLoader classLoader) {
        try {
            Class<?> klass = classLoader.loadClass(name);
            return getFragment(klass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BaseFragment getFragment(Class<?> klass) {
        try {
            //反射出插件的Fragment对象
            Constructor<?> localConstructor = klass.getConstructor();
            Object instance = localConstructor.newInstance();
            return (BaseFragment) instance;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String md5(File file) {
        try {
            return md5(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    public static String md5(InputStream inputStream) {
        try {
            byte[] buffer = new byte[8192];
            int len = 0;
            MessageDigest md = MessageDigest.getInstance("MD5");
            while ((len = inputStream.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeSilently(inputStream);
        }
        return null;
    }

}
